var ns;
var app = new Vue({
    el: '#app',
    data: {
        productlist: [
            {
                imageurl:'https://test.saasyy.com/upload/WechatServiceImg/20220610/f7e5d0e60f7940e3a30d0de47c5143c8_87f78367ee34cc808e359acc8e58b740.jpg',
                title:'国产 红心火龙果 3个装 总重量350-500g',
                price:'12.90',
                VariantInventory:3,
                SaleCount:108,
                SaleAmount:'2083.15'


        }],
        Variantproductlist:[],
        Variant:{
          VariantPrice:0,
          Inventory:0,
          VariantID:0
        },

        //参数
        pageParam: {
            status: 0, //状态
            sortby: 1, //排序
            keywords: '', //条件、
            // isprimary: false,
            IsAllowProductBuy:1,
            pageindex: 0,
            pagesize: 20,
        },
        form:{
             productId: '',
             title: '',
             price:0,
             xiaoLiang: '',
             kuChun:0,
             allPrice: '',
             imageUrl: '',
             shuxingId:'',//当前商品的属性值
             proTypeData: [],
         }
    },

    methods: {
        // get_data:function(){
        // var that = this;
        // that.is_searching = true;
        // var options = {};
        // options.action = 'getproductlistbybusinessid';
        // options.keywords = '';
        // options.sortby = that.pageParam.sortby;
        // options.status = that.pageParam.status;
        // options.pageindex = that.pageParam.pageindex;
        // options.pagesize = that.pageParam.pagesize;
        // options.IsAllowProductBuy = that.pageParam.IsAllowProductBuy;
        // ns.post(options, function(succeed, data, err) {
        //     that.is_searching = false;
        //     if (succeed) {
        //         that.productlist = data.list;
                // if (that.form.pageindex == 0) {
                //     that.productlist = data.list;
                // } else {
                //     that.productlist = that.productlist.concat(data.list);
                // }
                //that.current_item_length = that.list.length;
    //         } else if (err) {
    //             that.list = [];
    //             api.toast({
    //                 msg: err,
    //                 duration: 2000,
    //                 location: 'bottom'
    //             });
    //         }
    //     }, {
    //         toast: false
    //     });
    //   },  
      //调整
        //上架-----审核中
        onshelf: function(id) {
            var that = this;
            api.confirm({
                title: '提示',
                msg: '确定上架?',
                buttons: ['确定', '取消']
            }, function(ret, err) {
                if (ret.buttonIndex == 1) {
                    //调用上架接口
                    var options = {};
                    options.action = 'saveproductstatus';
                    options.productid = id;
                    options.status = 3;
                    ns.post(options, function(succeed, data, err) {
                        //alert(JSON.stringify(data));
                        if (succeed) {
                          api.toast({
                              msg: "修改成功",
                              duration: 1000,
                              location: 'bottom'
                          });
                        //   app.get_data();
                          var  menuPopup = new auiPopup();
                          var center = $api.byId('center');
                          menuPopup.hide(center);
                        } else if (err) {
                            api.toast({
                                msg: err,
                                duration: 2000,
                                location: 'bottom'
                            });
                        }
                    }, {
                        toast: false
                    });
                }
            });
        },
        //下架
        offshelf: function(id) {
            var that = this;
            api.confirm({
                title: '提示',
                msg: '商品下架后，买家不能再进行商品购买操作，需要重新上架才能继续购买，是否确认下架?',
                buttons: ['确定', '取消']
            }, function(ret, err) {
                if (ret.buttonIndex == 1) {
                    //调用商品下架接口
                    var options = {};
                    options.action = 'saveproductstatus';
                    options.productid = id;
                    options.status = 2;
                    ns.post(options, function(succeed, data, err) {
                        //alert(JSON.stringify(data));
                        if (succeed) {
                          api.toast({
                              msg: "修改成功",
                              duration: 1000,
                              location: 'bottom'
                          });
                        //   app.get_data();
                          var  menuPopup = new auiPopup();
                          var center = $api.byId('center');
                          menuPopup.hide(center);
                        } else if (err) {
                            api.toast({
                                msg: err,
                                duration: 2000,
                                location: 'bottom'
                            });
                        }
                    }, {
                        toast: false
                    });
                }
            });

        },
        //编辑
        editShop: function(id) {
            var that = this;
            var that = this;
            ns.openWin('dl_addproduct', '编辑商品', {
                id: id,
            });
        },
        //取消申请
        cancelApply: function(id) {
            var that = this;
            api.confirm({
                title: '提示',
                msg: '取消审核后，商品将返回未上架状态，是否继续?',
                buttons: ['确定', '取消']
            }, function(ret, err) {
                if (ret.buttonIndex == 1) {
                    //调用取消申请接口
                    var options = {};
                    options.action = 'saveproductstatus';
                    options.productid = id;
                    options.status = 2;
                    ns.post(options, function(succeed, data, err) {
                        //alert(JSON.stringify(data));
                        if (succeed) {
                          api.toast({
                              msg: "修改成功",
                              duration: 1000,
                              location: 'bottom'
                          });
                        //   app.get_data();
                          var  menuPopup = new auiPopup();
                          var center = $api.byId('center');
                          menuPopup.hide(center);
                        } else if (err) {
                            api.toast({
                                msg: err,
                                duration: 2000,
                                location: 'bottom'
                            });
                        }
                    }, {
                        toast: false
                    });
                }
            });
        },
        //新增
        open_addproduct: function(item) {
            var that = this;
            ns.openWin('dl_addproduct', '添加商品', {
                id: 0,
            });
        },
        //调整取消功能
        close_popup: function() {
          var  menuPopup = new auiPopup();
          var center = $api.byId('center');
          menuPopup.hide(center);
          return;
        },
        //调整确定功能
        do_popup:function(){
          var that = this;
          var options = {};
          options.action = 'saveproductvariant';
          options.VariantID = that.Variant.VariantID;
          options.VariantPrice = that.Variant.VariantPrice;
          options.Inventory = that.Variant.Inventory;
          ns.post(options, function(succeed, data, err) {
              //alert(JSON.stringify(data));
              if (succeed) {
                api.toast({
                    msg: "修改成功",
                    duration: 1000,
                    location: 'bottom'
                });
                // app.get_data();
                var  menuPopup = new auiPopup();
                var center = $api.byId('center');
                menuPopup.hide(center);
              } else if (err) {
                  api.toast({
                      msg: err,
                      duration: 2000,
                      location: 'bottom'
                  });
              }
          }, {
              toast: false
          });
        },
        //获取子商品
        get_VariantByProductId:function(id){
          var that=this;
          var options = {};
          options.action = 'getproductvariantbyproductid';
          options.productid = id;
          ns.post(options, function(succeed, data, err) {
              //alert(JSON.stringify(data));
              if (succeed) {
                  that.Variantproductlist = data;
                  //alert(JSON.stringify(that.Variantproductlist));
                  for (var i = 0; i < that.Variantproductlist.length; i++) {
                  that.Variant.VariantPrice=  that.Variantproductlist[0].VariantPrice;
                  that.Variant.Inventory=  that.Variantproductlist[0].Inventory;
                  that.Variant.VariantID=that.Variantproductlist[0].ID;
                  }

              } else if (err) {
                  api.toast({
                      msg: err,
                      duration: 2000,
                      location: 'bottom'
                  });
              }
          }, {
              toast: false
          });
        },
        //子商品选择事件
        changeVariant:function(id){
          var that=this;
          for (var i = 0; i < that.Variantproductlist.length; i++) {
            if(id== that.Variantproductlist[i].ID)
            {
              that.Variant.VariantPrice=  that.Variantproductlist[i].VariantPrice;
              that.Variant.Inventory=  that.Variantproductlist[i].Inventory;
              that.Variant.VariantID=that.Variantproductlist[i].ID;
            }
          }
        },
        editStock:function(id){
              var that = this;
              var  menuPopup = new auiPopup();
              var center = $api.byId('center');
              menuPopup.show(center);
              //弹出窗口,赋值
              this.get_VariantByProductId(id);
          },
    }
});
apiready = function() {
    api.parseTapmode();
    ns = window.Foresight.Util;
    //alert(api.pageParam.status);
    app.pageParam.status = api.pageParam.status;
    app.pageParam.sortby = api.pageParam.sortorder;
    // app.get_data();
    api.addEventListener({
        name: 'reloadclassproductlist'
    }, function(ret, err) {
        if (ret) {
            if (ret.value.status) {
                app.pageParam.status = ret.value.status;
            }
            // app.get_data();
        }
    });
    api.addEventListener({
        name: 'reloadproductlist'
    }, function(ret, err) {
        if (ret) {
            if (ret.value.sortOrder) {
                app.pageParam.sortby = ret.value.sortOrder;
            }
            // app.get_data();
        }
    });
};
